package lian_xi;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class YootkDemo2 {
    //使用原子类解决数据同步问题
    public static AtomicInteger money = new AtomicInteger();

    public static void main(String[] args) throws InterruptedException {
        int[] data = new int[]{100,200,300};
        for (int i = 0; i < data.length; i++) {
            final  int temp = i;
            new Thread(()->{
                try {

                    TimeUnit.MICROSECONDS.sleep(100);
                }catch (InterruptedException e){
                    money.addAndGet(data[temp]);
                }
            }).start();

        }
        TimeUnit.SECONDS.sleep(1);

        System.out.println("[计算完成] 最终存款总额：" + money);
    }
}
